package com.maniu.fluttermaniu;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

//native 层渲染   到  FlutterSurfaceView
public class FlutterSurfaceView  extends SurfaceView implements SurfaceHolder.Callback {
    private FlutterJNI flutterJNI = new FlutterJNI();
    public FlutterSurfaceView(Context context) {
        super(context);
    }

    public FlutterSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);

    }

    public FlutterSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
//        利用Skia引擎 帮助我们渲染    surface   做到这一点        理论 实践起来 没问题
//         JNI函数   --> native

//        new MyThread().start();
        drawSkia("david love 99");
    }
    private void drawSkia(String data) {
        Surface surface= getHolder().getSurface();
        flutterJNI.init(surface, getWidth(), getHeight());
        flutterJNI.drawText(data, 350, 150);
        Bitmap bitmap = BitmapFactory.decodeFile(new File(Environment.getExternalStorageDirectory(), "abc.png").getAbsolutePath());
        Log.d("david", "---------->" + bitmap);
        flutterJNI.drawBitmap(bitmap, 350, 200);
        flutterJNI.flush();
    }
    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
//            每隔0.5s调用一次
            File file = new File(Environment.getExternalStorageDirectory(), "dart.txt");

            String data = readString(file.getAbsolutePath());
            Log.d("david", "handleMessage: "+data);
            if (!TextUtils.isEmpty(data)) {

                drawSkia(  data);
            }
        }
    };

    public class MyThread extends Thread {
        @Override
        public void run() {
            while (true) {

                try {
                    Thread.sleep(1500);//线程暂停10秒，单位毫秒
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);//发送消息
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public String readString(String path) {
        //FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流，请考虑使用 FileReader。
        FileInputStream inStream = null;
        try {
            inStream = new FileInputStream(path);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];

            int length = -1;

            while ((length = inStream.read(buffer)) !=-1)
            {
                bos.write(buffer, 0, length);
            }
            bos.close();
            inStream.close();
            return bos.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
